package Cliente.ventanas;

import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.IOException;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import Cliente.TCPCliente;

public class InfoTrenPanel extends JPanel implements MouseListener, ActionListener{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private int fin;
	private JLabel labelID;
	private JLabel labelEstado;
	private JLabel labelGPS;
	private JButton sensores;
	private JTextField fieldID;
	private JTextField fieldEstado;
	private JTextField fieldPosition;
	
	private JRadioButton botonGPS;
	private int vehic;
	private JLabel trenImg ;

	TCPCliente tcp;
	
	public InfoTrenPanel(MainWindow main, int vehiculo, TCPCliente tcp){
		botonGPS = new JRadioButton("Off", false);
		this.tcp = tcp;
		vehic=vehiculo;
		Font fuente= new Font("Arial", 0, 15);
		String message = null;
		try {
			tcp.sentMessage("SWITCHTRAIN "+vehiculo+"\n");
			message=tcp.receiveMessage();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		sensores = new JButton("Sensores");
		
		trenImg = new JLabel(new ImageIcon("src/Img/funcionando.gif"));
		
		fieldID = new JTextField();
		fieldEstado = new JTextField();
		fieldPosition = new JTextField();
//		System.out.println(message);
		fieldID.setText(ObtenerSubString(message, ';', 21));
		fieldEstado.setText(ObtenerSubString(message, ';', fin+1));
		if(message.substring(fin+1, message.length()).equals("true"))	{	
			botonGPS.setText("On");
			GET_LOC();
			//ONGPS();
			botonGPS.setSelected(true);
		}
		else{
			botonGPS.setText("Off");
			botonGPS.setSelected(false);
		}
		fieldID.setEditable(false);
		fieldEstado.setEditable(false);
		fieldPosition.setEditable(false);
			
		
		labelID = new JLabel("ID :");
		labelID.setFont(fuente);
		
		labelEstado = new JLabel("Estado :");
		labelEstado.setFont(fuente);
		
		labelGPS = new JLabel("Estado GPS :");
		labelGPS.setFont(fuente);
		
		
		botonGPS.setFont(fuente);
		
		botonGPS.addMouseListener(this);
		
		this.setSize(500,400);
		this.setVisible(true);
		this.setLayout(null);
		
		this.add(labelID);
		this.add(labelEstado);
		this.add(labelGPS);
		
		
		this.add(fieldEstado);
		this.add(fieldID);
		this.add(botonGPS);
		this.add(fieldPosition);
		
		this.add(sensores);
		
		sensores.addActionListener(this);
		
		this.add(trenImg);
		
		trenImg.setBounds(350, 40, 200, 150);
		
		labelID.setBounds(30, 50, 80, 25);
		fieldID.setBounds(150, 50, 100, 30);
		labelEstado.setBounds(30, 120, 100, 25);
		fieldEstado.setBounds(150, 120, 140, 30);
		labelGPS.setBounds(30, 190, 130, 25);
		botonGPS.setBounds(150, 190, 130, 30);
		fieldPosition.setBounds(30, 225, 300, 30);
		sensores.setBounds(100, 280, 100, 50);
		
	}


	@Override
	public void mouseClicked(MouseEvent e) {
		// TODO Auto-generated method stub
//		System.out.println("Mouse click");
		if(botonGPS.isSelected()){
			ONGPS();
			GET_LOC();
			botonGPS.setText("On");
				
		}
		else{
				OFFGPS();
				botonGPS.setText("Off");
		}
	
	}

	@Override
	public void mousePressed(MouseEvent e) {
		// TODO Auto-generated method stub
	
	}


	@Override
	public void mouseReleased(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}


	@Override
	public void mouseEntered(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}


	@Override
	public void mouseExited(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}


	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		try{
			new SensorWindow(Integer.parseInt(fieldID.getText()), tcp);
		}catch(Exception exception){System.out.println("No se a podido abrir la ventana. El tren no tiene sensores.");};

	}
	
	public String ONGPS(){
		try {
			tcp.sentMessage("ONGPS\n");
//			System.out.println(+"   On"+this.getVehic());
			tcp.receiveMessage();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "";
	}
	
	public String OFFGPS(){
		try {
			tcp.sentMessage("OFFGPS\n");
			System.out.println(tcp.receiveMessage()+"   Off"+this.getVehic());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "";
	}
	
	public void GET_LOC(){
		try {
			tcp.sentMessage("GET_LOC\n");
			String loc="No recibido";
			loc= tcp.receiveMessage();
			loc=loc.substring(7);
			System.out.println("Metodo getLoc: "+loc);
			fieldPosition.setText(loc);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public String ObtenerSubString(String message, char marca, int inicio){
		char ch = 0;
		int finID =0;
		for(int j = inicio;ch != marca&&j<message.length();j++){
			ch = message.charAt(j);
			finID = j;
			fin = j;
		}
		System.out.println("Menssage: " + message);
		System.out.println(inicio+"____"+finID);
		return message.substring(inicio, finID);
	}
	
	public void actualizarVentana(String id, String estado,String estadoGps){
		fieldEstado.setText(estado);
		trenImg.setIcon(new ImageIcon("src/Img/"+estado+".gif"));
//		trenImg = new JLabel(new ImageIcon("src/Img/"+estado+".gif"));
//		trenImg.repaint();
		if(estadoGps.equals("true"))
		{
			botonGPS.setText("On");
			GET_LOC();
			ONGPS();
			botonGPS.setSelected(true);
		}
		else{
			botonGPS.setText("Off");
			botonGPS.setSelected(false);
		}
	}


	public int getFin() {
		return fin;
	}


	public int getVehic() {
		return vehic;
	}
	
	}
	
